﻿@using System.Data;
@{
    ViewBag.Title = "Search";
}

<h2>Search</h2>

<p>@ViewBag.SearchMessage</p>

<div class="row">
    <div class="span9">
@if (Model != null)
{ 
<table class="neat">
    <caption>@Model.Rows.Count search results</caption>
    <thead>
        <tr>
            <th>Name</th>
            <th>Identifier</th>
            <th>Country Code</th>
            <th>Market Cap</th>
            <th>Holdings</th>
            <th>Fund Sec No</th>
            <th>&nbsp;</th>
        </tr>
    </thead>
    <tbody>
@foreach (System.Data.DataRow r in Model.Rows)
{
    <text>
        <tr data-primaryidentifier="@r["PrimaryIdentifier"]">
            <td onclick="view();">@r["Name"]</td>
            <td>@r["PrimaryIdentifier"]</td>
            <td>@r["CountryCode"]</td>
            <td class='@DataHelper.GetDataTypeCss(Model.Columns["MarketCap"])'>@DataHelper.GetData(r["MarketCap"], "F2", (string)"NA")</td>
            <td>@r["Holdings"]</td>
            <td>@r["FundSecNo"]</td>
            <td>
                Fcst 
                <span onclick="viewFundamentals();">Fdmt</span>
                Swp
            </td>
        </tr>
    </text>
}
    </tbody>
</table>
}
else { 
    <p>No records found.</p>
}

    </div>

    <div id="resizable" class="span3">
        <div style="border: 1px double #ccc;">
            <h4>Actions</h4>
            <ul>
                <li>View security forecast</li>
                <li>View fundamentals</li>
                <li>View swaps</li>
            </ul>
        </div>
    </div>
  
</div>


<div id="dialog" title="Actions">
    <p>
        Obagi Medical Products, Inc. (Obagi) is a specialty pharmaceutical company that develops, markets and sells, and is a provider of, topical aesthetic and therapeutic prescription-strength skin care systems and related products in the physician-dispensed market. Obagi systems and products are designed to prevent and improve the common and visible skin disorders in adult skin, including premature aging, photodamage, hyperpigmentation (irregular or patchy discoloration of the skin), acne, sun damage, facial redness, and soft tissue deficits, such as fine lines and wrinkles. Obagi sells its products through a direct sales force in the United States and internationally through distribution partners in over 43 countries across North America, Central America, Europe, the Middle East and Asia. Its domestic sales force and foreign distributors sell its products directly to dermatologists, plastic surgeons and other physicians who are focused on aesthetic and therapeutic skin care.</p>
</div>

<div id="fundamentalsDialog" title="Fundamentals">
    <div id="fundamentalsForm"></div>
    <div id="fundamentalsChart">
        <p>CHart Placeholder</p>
    </div>
    <div id="fundamentalsTable">
        <p>Table </p>
    </div>
</div>

<p style="height:600px;"></p>

@*<div>
    <input type="button" value="Hide Dialog" onclick="$('#dialog').wijdialog('close')" />
    <input type="button" value="Show Dialog" onclick="$('#dialog').wijdialog('open')" />
</div>
*@

@section scripts
{
@*    <script type="text/javascript" src="http://code.highcharts.com/stock/highstock.src.js"></script>
    <script type="text/javascript" src="http://code.highcharts.com/stock/highcharts-more.src.js"></script>
    <script type="text/javascript" src="http://code.highcharts.com/stock/modules/exporting.src.js"></script>*@

    <script type="text/javascript">

    function ajaxFailure(result, txtStatus, thrErr) {
        /// <summary>Ajax call failures handler</summary>
        var message = "Data retrieval failure!\r\nStatus code:" + result.status + "\r\nStatus: " + result.statusText + "\r\nDescription:\r\n";
        var json = null;

        json = $.parseJSON(result.responseText);
                
        if (json !== null) {
            if (json.Message !== undefined) {
                var jsonMessage = $.parseJSON(json.Message);

                if (jsonMessage !== null) {
                    $("td.sec" + jsonMessage.SecNum).html("ERROR");
                    loadedSecurities[jsonMessage.SecNum] = true;

                    var allLoaded = true;
                    for (var secIndex = 0; secIndex < loadedSecurities.length; secIndex++) {
                        if (loadedSecurities[secIndex] === false) {
                            allLoaded = false;
                            break;
                        }
                    }

                    if (allLoaded) {
                        $("#progressbar").html("");
                        $("#progressbar_loader").css("display", "none");
                    }

                    if (deltaMode) {
                        if (allLoaded) {
                            loadDeltaData();
                        }
                    }

                    message = message + json.Message;
                } else {
                    message = json.Message;
                }
            } else {
                message = message + result.responseText;
            }
        } else {
            message = message + result.responseText;
        }

        window.alert(message);
    }

    function makeFundamentalMarkup(data)
    {
        debugger;
        // Formulate markup

        // Display dialog
        jQuery("#fundamentalsDialog").wijdialog('open');
    }
    
    function viewFundamentals()
    {
        debugger;
        // Make AJAX call to retrieve data
        var jsonData = "{ 'primaryIdentifier': 'toyota'}";
        $.ajax({
            async: false,
            type: "POST",
            url: '@Url.Action("GetFundamentals", "FinancialData")',
            data: jsonData,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: makeFundamentalMarkup,
            error: ajaxFailure
        });
    }

    function view() {
        jQuery('#dialog').wijdialog('open');
    }

    (function ($) {
        "use script";
        $(document).ready(function () {

            $( "#resizable" ).resizable({
                ghost:true,
                grid: [ 20, 10 ],
                minWidth: 150,
                minHeight: 300,
                maxHeight: 500,
                handles: "s"
            });

            $("#fundamentalsDialog").wijdialog({
                autoOpen: false,
                captionButtons: {
                    refresh: { visible: false }
                },
                open: function (e, a, b) {
                    debugger;
                },
                position:  'center',
                zIndex: 9999,
            });

            $('#dialog').wijdialog({
                autoOpen: false,
                captionButtons: {
                    refresh: { visible: false }
                },
                open: function (e, a, b) {
                    debugger;
                },
                position:  ['right','top'],
                zIndex: 9999,
            });
        });
    } (jQuery));
    </script>
}